//
//  UIViewController+NacView.m
//  明和智能
//
//  Created by 李李善 on 2018/8/24.
//  Copyright © 2018年 inno. All rights reserved.
//

#import "UIViewController+NacView.h"
#import <objc/runtime.h>
static char shadowImage;

@implementation UIViewController (NacView)


-(UIImage *)shadowImage
{
    return objc_getAssociatedObject(self,&shadowImage);
}

- (void)setShadowImage:(UIImage *)shadowImage
{
    objc_setAssociatedObject(self, &shadowImage, shadowImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setNavigationBarTranslucent:(BOOL)translucent{
    
    if (self.navigationController == nil) return;
    
    if (translucent) {
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
        
        self.navigationController.navigationBar.translucent = YES ;
        
        [self.navigationController.navigationBar setBackgroundImage:[self imageWithBgColor:RGBA(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[self imageWithBgColor:RGBA(0, 0, 0, 0)]];
        
    } else {
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
        
        self.navigationController.navigationBar.translucent = NO;
        
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:173/255.0f green:209/255.0f blue:129/255.0f alpha:1];
        [self.navigationController.navigationBar setShadowImage:self.shadowImage];
    }
    
    
}

- (void)setNavigationBarWithAlpha:(CGFloat)alpha{
    if (alpha > 1) {
        alpha = 0.98;
    }
    if (alpha < 0.1) {
        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
        [self.navigationController.navigationBar setBackgroundImage:[self imageWithBgColor:RGBA(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:[self imageWithBgColor:RGBA(0, 0, 0, 0)]];
        
    } else if (alpha > 0.1) {
        self.navigationController.navigationBar.tintColor = [UIColor blackColor];
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blackColor]}];
        
        [self.navigationController.navigationBar setBackgroundImage:[self imageWithBgColor:RGBA(255, 255, 255, alpha)] forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:self.shadowImage];
        
    }
    
}


-(UIImage *)imageWithBgColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}
@end
